define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/devicefill/index' + location.search,
                    add_url: 'device/devicefill/add',
                    edit_url: 'device/devicefill/edit',
                    del_url: 'device/devicefill/del',
                    multi_url: 'device/devicefill/multi',
                    import_url: 'device/devicefill/import',
                    table: 'device_fill',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'device_id', title: __('设备ID')},
                        {field: 'num', title: __('补货数'),operate: false},
                        {field: 'create_time', title: __('时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user.mobile', title: __('补货人'),operate: false},
                        {field: 'name', title: __('补货标题'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },

                            buttons: [

                                {
                                    name: 'list',
                                    title: __('明细记录'),
                                    text: "明细记录",
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-bitcoin',
                                    url: function (row) {
                                        return "device/fillDetail/index?fill_id=" + row['id'];
                                    }
                                }
                            ]

                        }]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
